import mx.transitions.Tween;

class timber.sound.SoundFader{;
	

	private var whichSound:Object;
	private var vol:Number;
	private var pan:Number;
	private var oneoff:Boolean = false;
	private var fadeDelegate:Object;
	private var panDelegate:Object;
	
	function SoundFader(mysound:Sound){
		whichSound = mysound;
		trace("whichSound = " +whichSound);
	}

	public function fadeTo(fadeDest:Number,fadeTime:Number,easingEQ,false,mydelegate){
		vol = whichSound.getVolume();
		if(mydelegate) fadeDelegate = mydelegate;
		var fadeTween = new Tween(this,"vol",easingEQ,vol,fadeDest,fadeTime,true);
		fadeTween.self = this;
		fadeTween.sound = whichSound;
		fadeTween.onMotionChanged = function(){
			this.sound.setVolume(this.self.vol);
		}
		fadeTween.onMotionFinished = function(){
			this.self.fadeDelegate.apply();
			if(this.self.oneoff) this.self.destroy();
		}
	}
	
	public function panTo(fadeDest:Number,fadeTime:Number,easingEQ,mydelegate){
		pan = whichSound.getPan();
		if(mydelegate) panDelegate = mydelegate;
		var panTween = new Tween(this,"pan",easingEQ,pan,fadeDest,fadeTime,true);
		panTween.self = this;
		panTween.sound = whichSound;
		panTween.onMotionChanged = function(){
			this.sound.setPan(this.self.pan);
		}
		panTween.onMotionFinished = function(){
			this.self.panDelegate.apply();
			if(this.self.oneoff) this.self.destroy();
		}		
	}
	
	public function setOneOff(bool:Boolean){
		oneoff = bool;
		return oneoff;
	}
	
	public function getOneOff(){
		return oneoff;
	}
	
	public function destroy(){
		delete this;
	}
}
